package com.cherrypt.api.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cherrypt.api.core.dao.DiscountDao;
import com.cherrypt.api.core.service.DiscountService;
import com.cherrypt.api.model.dataobject.Discount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * @author intent
 * @date 2019/7/23 20:01
 * @about <link href='http://zzyitj.xyz/'/>
 */
@Service
public class DiscountServiceImpl extends ServiceImpl<DiscountDao, Discount> implements DiscountService {
    @Autowired
    private ValueOperations<String, Object> valueOperations;

    @Value(value = "${api.config.redis.discount.prefix}")
    private String discountPrefix;

    @Override
    public void saveRedisDiscount(Discount discount) {
        valueOperations.set(discountPrefix + discount.getId(), discount);
    }

    @Override
    public Discount getRedisDiscount(int discountId) {
        return (Discount) valueOperations.get(discountPrefix + discountId);
    }

    @Override
    public Discount getDiscount(int discountId) {
        Discount discount = getRedisDiscount(discountId);
        if (discount == null) {
            discount = getById(discountId);
            if (discount != null) {
                saveRedisDiscount(discount);
            }
        }
        return discount;
    }
}
